<?php

include_once 'companyProduct.class.php';
class companys extends common{
	private $companies_table = "companies";
	private $companies_detail_title_table = "companies_detail_title";
	private $companies_detail_table = "companies_detail";
	private $companies_level_table = "companies_level";
	private $companies_trade_table = "companies_trade";
	private $user_investment_table = "user_investment";
	private $companies_click_table = "companies_click";
	private $companies_categories_table = "companies_categories";
      
        function getCompanyInfo($param){
		extract($param);
		if ($company_id != null && $company_id!= "") {
			$sql = $this->companies_table." c left join ".$this->companies_level_table." l on c.level_id=l.ID left join ".$this->companies_trade_table." t on c.trade_id=t.id where c.id=".$company_id;
			$res = $this->getinfo($sql,'c.id,c.title,c.categoty_id,c.star,l.score,t.title as company_type');
		}

		if($res['id']){
			$sql = $this->companies_detail_table." d
					left join ".$this->companies_detail_title_table." t on d.title_id=t.id
					where d.company_id='".$res['id']."'";
			$info = $this->getList($sql,'t.id_marking,d.content');
			if(is_array($info['result'])){
				foreach($info['result'] as $key=>$val){
					$res[$val['id_marking']] = $val['content'];
				}
			}
		}
               
		return $res; 
	}
	/**
	 *获取公司列表
	 * @param	string	$type
	 * @param	string	$key
	 * @param	int		$num
	 * @param	int		$page
	 * @return	array 
	*/
	function getCompanyList($param){
		extract($param);
		$sql = $this->companies_table." c left join ".$this->companies_level_table." l on c.level_id=l.ID where c.is_top=1";
		$res = $this->getList($sql,'c.id,c.title,c.star,c.categoty_id,l.score,c.img,c.province,c.cirty,c.county',$num,$page);

		if(is_array($res['result'])){
			foreach($res['result'] as $k=>$v){
				$sql = $this->companies_detail_table." d
						left join ".$this->companies_detail_title_table." t on d.title_id=t.id
						where d.company_id='".$v['id']."'";
				$info = $this->getList($sql,'t.id_marking,d.content');
				if(is_array($info['result'])){
					foreach($info['result'] as $key=>$val){
						$v[$val['id_marking']] = $val['content'];
					}
				}

				$res['result'][$k] = $v;
			}
		}
		return $res['result'];
	}
	/**
	 *获取公司详情
	 * @param	
	 * @param
	*/
	function getCompanyDetail($param){
		extract($param);
		if($companyid!='' && $companyid!=null){
			$sql  = $this->companies_table." com left join ".$this->companies_detail_title_table." cdt on com.id=$companyid left join companies_detail cd on cdt.id = cd.title_id and cd.company_id = ".$result['company_id']."$companyid";
                             $key = "cdt.id_marking,cd.content";
                             $list2 =$this->getList($sql, $key);
			if(is_array($list2['result']))
                        {      
				foreach( $list2['result'] as $key=>$val)
                                { 
					$result[$val['id_marking']] = $val['content'];		
				}                   
			}
			//获取公司所属一级分类
			$sql4=$this->companies_table." c left join ".$this->companies_categories_table." g on c.upid=g.sort where c.id=".$companyid;
			$cate=$this->getList($sql4, $key="g.title as categories");
			if(is_array($cate['result'])){
				foreach($cate['result'] as $keys=>$value){
					
				}
			}
                        //获取浏览次数
                        $sql5=$this->table_name." where company_id=$companyid and state=5";
			$seenum = $this->getNum($sql5);
                        //获取举报次数
                         $sqlR="  user_reported  where company_id=$companyid ";
			$reportednum = $this->getNum($sqlR);
			//获取点赞量
			$sql1=$this->table_name." where company_id=$companyid and state=3";
			$click=$this->getNum($sql1);
				
			//获取收藏量
                        $sql2=$this->user_investment_table." where state=4 and company_id=$companyid ";			
			 $save=$this->getNum($sql2);
				
			//获取投资人数
                        $sql3=$this->user_investment_table." where state=1 and company_id=$companyid ";			
			 $invest=$this->getNum($sql3);
				
			//公司名称、图片
                        $sql4=$this->companies_table." where id=$companyid";
                        $companyName=$this->getInfo($sql4,$key='title,img as companyPicture,businesslicense,legalperson,star,province,cirty,county',$debug='0');
       
				$result1=array('getCompanyClickNum'=>"$click",
				               'companySaveNum'=>"$save",
							   'companyInvestNum'=>"$invest",'seeNum'=>$seenum,'reportedNum'=>$reportednum);	
                if(is_array($companyName)){							   
				$merge=array_merge($result,$result1,$companyName,$value);
				}else{
					$msg="此公司不存在";
					$error="0";
				}
		}
                 $product = newInstance('companyProduct');
                $productResult = $product->getProductList($param);
		return array('companyid'=>$companyid,'merge'=>$merge,'productList'=>$productResult['result']);
	}
	/**
	 *用户对公司点赞功能的实现
	 * @param	
	 * @param
	*/
	private $table_name="user_investment"; 
	function CompanyClick($param){
		extract($param);
		if($userid!="" && $companyid!=null){
			//去数据库查询此用户是否已经点赞
			$sql=$this->table_name." where user_id=$userid and company_id=$companyid and state=3";
			$result=$this->getInfo($sql);
			if($result!="" && $result!=null){
				$msg="已经取消对该公司点赞";
				$error="1";
				/*$datas=array('state'=>'4');
				$this->update($this->table_name,$datas,"company_id=$companyid");*/
				$sqls="delete from user_investment where user_id=$userid and company_id=$companyid and state=3";
				$resu = $this->query($sqls);
				
			}else{
				$msg="点赞成功";
				$error="0";
				$data = array('user_id'=>"$userid",'company_id'=>"$companyid",'state'=>3,'click_date'=>date('Y-m-d',time()));
				$res=$this->insert($this->table_name,$data);	
			}
		}
		return array('msg'=>$msg,'error'=>$error,'state'=>$data['state']);
	}
        /**
         * 增加浏览次数
         * state: 1 投资 3 点赞 4 收藏 5 浏览
         *     
         */
        function addSeeNum($param){
		extract($param);
		if($companyid!=null){
			//去数据库查询此用户是否已经点赞
			
			$msg="点赞成功";
			$error="0";
			$data = array('user_id'=>"$userid",'company_id'=>"$companyid",'state'=>5,'click_date'=>date('Y-m-d',time()));
			$res=$this->insert($this->table_name,$data);	
		}
		return array('msg'=>$msg,'error'=>$error,'state'=>$data['state']);
	}
	//获取点赞的个数
	function getCompanyClick($param){
		extract($param);
		if($companyid!='' && $companyid!=null){
			$sql=$this->table_name." where company_id=$companyid and state=3";
			$result=$this->getNum($sql);
			return array('result'=>$result);
		}	
	}
	
	//获取点赞状态
        function  getClickState($param) {
          extract($param);
		  //去数据库查询此用户是否已经收藏
			$sql=$this->table_name." where user_id=$userid and company_id=$companyid and state=3";
			$result=$this->getInfo($sql);
			if($result['state']==3){		
					return array('msg'=>$msg,'error'=>$error,'state'=>'3');
			}else{
				    return array('msg'=>$msg,'error'=>$error,'state'=>'0');
			}
        }
	
	
	//根据一级分类查三级分类
	function strThird($param){
		extract($param);
		if($catefrist!='' && $catefrist!=null){
			if($catesecond!='' && $catesecond!=null){	
				$sqls=$this->companies_table." c left join ".$this->companies_categories_table." g on c.upid=g.sort where g.sort=".$catesecond;
				$res=$this->getList($sqls,'c.title,c.id,c.img,c.star',$num,$page);
				if(is_array($res['result'])){
					foreach($res['result'] as $k=>$v){
						$sql = $this->companies_detail_table." d
								left join ".$this->companies_detail_title_table." t on d.title_id=t.id
								where d.company_id='".$v['id']."'";
						$info = $this->getList($sql,'t.id_marking,d.content');
						if(is_array($info['result'])){
							foreach($info['result'] as $key=>$val){
								$v[$val['id_marking']] = $val['content'];
							}
						}

						$res['result'][$k] = $v;
					}
				}
				return $res;
				
			}else{
				$sql=$this->companies_table." c left join ".$this->companies_categories_table." g on c.upid=g.sort where g.upid=".$catefrist;
				$row=$this->getList($sql,'c.title,c.id,c.img,c.star',$num,$page,$debug='0');
				if(is_array($row['result'])){
					foreach($row['result'] as $k=>$v){
						$sql = $this->companies_detail_table." d
								left join ".$this->companies_detail_title_table." t on d.title_id=t.id
								where d.company_id='".$v['id']."'";
						$info = $this->getList($sql,'t.id_marking,d.content');
						if(is_array($info['result'])){
							foreach($info['result'] as $key=>$val){
								$v[$val['id_marking']] = $val['content'];
							}
						}
						$res['result'][$k] = $v;
					}
				}
						
				return $res;
			}
		}
	}
	//获取首页图片
	function firstAdver($param){
		extract($param);
		$sql=$this->companies_table." where is_img=1 limit 0,1";
		$result=$this->getInfo($sql,'img,id');
		return $result;
	}
	//返回链接地址
	function share($param){
		extract($param);
		$link="https://itunes.apple.com/cn/app/mei-tuan-tuan-gou-tuan-gou/id423084029?mt=8";
		if($link){
		return $link;
		}
	}
}
?>